package com.anlogic.sdk.utils.options;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.apache.logging.log4j.Logger;

import com.anlogic.sdk.utils.FileUtils;
import com.anlogic.sdk.utils.LogUtils;

public enum ProjectType {
	SOFTCORE("softcore", "softcore"), SF1("sf1", "sf1"), DR1M90("dr1m90", "dr1m90"), DR1V90("dr1v90", "dr1v90"),
	PH1P35("ph1p35", "ph1p35"), SOC66080("soc66080", "soc66080"), DR1M350("dr1m350", "dr1m350");

	private static Logger logger = LogUtils.getLogger(ChipType.class);
	
	private String id;
	
	private String name;

	private static Map<String, ProjectType> idMap = new LinkedHashMap<>();

	private static Map<String, ProjectType> nameMap = new LinkedHashMap<>();
	
	static {
		Properties prop = new Properties();
		InputStream input = null;
		try {
			String path = FileUtils.getPluginResourcePath("com.anlogic.sdk.utils", "chip.properties");
		    input = new FileInputStream(path);
		    prop.load(input);
		} catch (Exception e) {
			logger.error("Load chip properties error: {}", e.getMessage());
		} finally {
		    if (input != null) {
		        try {
		            input.close();
		        } catch (IOException e) {
		        	logger.error("Load chip properties error: {}", e.getMessage());
		        }
		    }
		}
		
		for (ProjectType value : ProjectType.values()) {
			String isVisiable = prop.getProperty(value.getName());
			if (isVisiable != null && isVisiable.equals("false")) {
				continue;
			}
			idMap.put(value.getId(), value);
			nameMap.put(value.getName(), value);
		}
	}

	ProjectType(String id, String name) {
		this.id = id;
		this.name = name;
	}

	public String getId() {
		return this.id;
	}

	public String getName() {
		return this.name;
	}

	public static boolean isValid(String name) {
		return nameMap.containsKey(name);
	}
	
	public static String[] getIds() {
		return idMap.keySet().toArray(new String[0]);
	}

	public static String[] getNames() {
		return nameMap.keySet().toArray(new String[0]);
	}

	public static ProjectType getTypeById(String id) {
		return idMap.get(id);
	}

	public static ProjectType getTypeByName(String name) {
		return nameMap.get(name);
	}

	public static String[] getCpuNames(String projType) {
		List<String> list = new ArrayList<>();
		if (projType.equals(SOFTCORE.getName()) || projType.equals(SF1.getName())) {
			list.add(CpuType.RPU.getName());
		} else {
			for (CpuType cpu : ChipType.getCpuList(projType)) {
				list.add(cpu.getName());
			}
		}
		return list.toArray(new String[0]);
	}
	
	public static List<String> getArchTypes(String projType) {
		List<String> list = new ArrayList<>();
		if (projType.equals(SOFTCORE.getName()) || projType.equals(SF1.getName())) {
			list.add(ArchType.RISCV.getName());
		} else {
			for (ArchType arch : ChipType.getArchList(projType)) {
				list.add(arch.getName());
			}
		}
		return list;
	}
	
	public static boolean isSoftcore(String name) {
		return name != null && !name.isEmpty() && SOFTCORE.getName().equals(name);
	}
	
	public static boolean isSf1(String name) {
		return name != null && !name.isEmpty() && SF1.getName().equals(name);
	}
	
	public static boolean isDr1m90(String name) {
		return name != null && !name.isEmpty() && DR1M90.getName().equals(name);
	}
	
	public static boolean isDr1v90(String name) {
		return name != null && !name.isEmpty() && DR1V90.getName().equals(name);
	}
	
	public static boolean isPH1P35(String name) {
		return name != null && !name.isEmpty() && PH1P35.getName().equals(name);
	}
	
	public static boolean isSOC66080(String name) {
		return name != null && !name.isEmpty() && SOC66080.getName().equals(name);
	}
	
	public static boolean isDr1m350(String name) {
		return name != null && !name.isEmpty() && DR1M350.getName().equals(name);
	}
	
	public static boolean isSOC(String name) {
		return name != null && !name.isEmpty() && (isDr1m90(name) || isDr1v90(name) || isPH1P35(name) || isSOC66080(name));
	}
}
